Skip to content

Conversation

@mattam82
Copy link
Member

@mattam82 mattam82 commented May 10, 2022

May 10, 2022

This PR adds a new loop-checking algorithm to MetaCoq, which should allow to handle arbitrary universe constraints efficiently, e.g. max (u + 1, v) = max (j, k+ 2). It is based on a constructive proof by Bezem et Coquand for the slightly more general problem of building a model in N^∞ of a set of horn clauses of shape u + k \/ ... \/ l + n -> l' + k'. This can be restricted to check if a loop follows from a set of constraints or if we have a model in N, which can in turn be seen as a valuation of the clauses making them all (non-vacuously) true.

The algorithm is currently only shown to be terminating and to return a valid model when it finds one, but it might also answer Loop, in which case we have not (yet) proven that there is indeed a loop.

I added a new extracted plugin in the test-suite (based on plugin-demo), that provides a new command MetaCoq Check Universes that checks for a model using the new algorithm on all the constraints in the global environment at the point of the call. It currently takes < 0.5s to verify that there is a valuation with 4 distinct universes for all the universes and constraints in Coq's standard library.

Nov 8, 2025

The PR is now complete with a correct and complete algorithm for MetaRocq handling all algebraic universes and an update of all the theory and quoting/denotation to support universe instances which actually contain arbitrary universes and not just levels.

It refines the algorithm of Bezem & Coquand to support a bottom universe (zero) and injective successor (so that i <= j <-> i + k <= j + k). The model is hence in Z to have a well behaved predecessor / substraction operation. We show that it is complete w.r.t. the same valuations used by MetaRocq earlier (with global universes level s > 0, local lvar n >= 0 and zero mapped to 0%Z). Completeness is based on a presentation of semilattices as an equational theory and the construction of its initial model (syntax/term model). The files in LoopChecking have documented headers explaining the inner working of the algorithm.

Re: changes to the API, for now I removed the consistent_extension_on support (checking we don't add global constraints), which needs redesign. The algorithm is written in highly dependent style to show termination, and does not compute inside Rocq itself. I think an extensionally equivalent fuel-based variant can be written easily for translations/examples that need computation inside Rocq.

mattam82 and others added 3 commits July 17, 2025 14:32
Terminating but incorrect loop check

Improved version, whose termination relies on the correctness of check_model

Move back to template-coq folder

Improve the algorithm after discussion with Marc Bezem

Comment a bit

Try enforcing new constraints

Reorganize inner loop

Cleaner inner_loop

Finished inner loop

Inner loop termination proven

Before change of v_minus_w_bound

Prove the well-foundedness of loop

Inner loop termination proof finished

Finished all proofs

Finish proofs of auxilliary lemmas

Finished all proofs with new invariants

Comments and extraction setup

Abstract LoopChecking on level / sets / maps implementation

Move clauses.v to LoopChecking and TemplateLoopChecking

Functorize the algorithm

Simplified loops

Cleaned up LoopChecking and TemplateLoopChecking

Support correct quoting/unquoting of the universe graph/context.
Also rename Constraint to LevelConstraint, preparing for a later move to general universe constraints

Add a new (extracted) plugin to test the loop checking algorithm.

Move back to subst_instance_cstr for level constraints

Move live tests of loop_checking to the test-suite

Revert changes to Universes.v

Avoid repeateadly folding over clauses in inner loop

Optimize a bit loop checking to avoid partitioning clause repeatedly

Fix UnivConstraint -> LevelConstraint change

Remove useless extraction file in template-coq

MSetList is no longer needed
@mattam82 mattam82 force-pushed the universes-clause-check branch from 2549615 to 2d178ab Compare July 18, 2025 23:02
@mattam82 mattam82 changed the title Universes clause check Algebraic universes everywhere Nov 8, 2025
@mattam82 mattam82 marked this pull request as ready for review November 8, 2025 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants